package com.bird.thread.base.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: bird
 * @Data: 2021/7/2 11:22
 * @description: sleep 不释放lock锁 lock锁需要手动的释放
 */
public class SleepDontReleaseLock implements Runnable {

    // lock锁对象
    private static final Lock lock = new ReentrantLock();


    public static void main(String[] args) {
        SleepDontReleaseLock sdr = new SleepDontReleaseLock();
        new Thread(sdr).start();
        new Thread(sdr).start();
    }

    @Override
    public void run() {
        // 加锁
        lock.lock();
        // 真正执行的逻辑
        System.out.println("线程：" + Thread.currentThread().getName() + "获取到了锁");

        try {
            Thread.sleep(5000);
            System.out.println("线程" + Thread.currentThread().getName() + "已经苏醒");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 释放锁
            lock.unlock();
        }
    }
    /*总结：sleep不会释放锁
     *
     *
     */
}
